package com.mk.cloud.pt.controller.app;

import act.apidoc.Description;
import act.controller.Controller;
import com.mk.cloud.common.domain.PBuyerAddress;
import com.mk.cloud.common.domain.PUser;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.header.HeaderService;
import com.mk.cloud.common.interceptor.header.IHLogin;
import com.mk.cloud.common.util.ResUtils;
import com.mk.cloud.common.util.S2;
import org.osgl.http.H;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.With;
import org.osgl.util.S;

import javax.inject.Inject;
import java.util.List;

@Controller("app/address")
public class PBuyerAddressController extends HeaderService {

    @Inject
    private PBuyerAddress.Mapper pBuyerAddressMapper;
    @Inject
    private PUser.Mapper pUserMapper;

    @Description("获取用户自己的收货地址信息")
    @GetAction("list.json")
    @With(IHLogin.class)
    public String buyerAddressList(
            H.Session session
    ){
        PBuyerAddress buyerAddress = new PBuyerAddress();
        buyerAddress.setFuserid(me.getFuserid());
        return ResUtils.OK(pBuyerAddressMapper.template(buyerAddress));
    }


    @Description("获取用户自己的收货地址信息")
    @PostAction("save.json")
    @With({ITransacte.class,IHLogin.class})
    public String saveBuyerAddress(
            H.Session session,
            PBuyerAddress buyerAddress){
//        PUser user = new PUser();
//        user.setFuserid(me.getFuserid());
//        user.setShopname(buyerAddress.getShopname());
//        pUserMapper.updateTemplateById(user);
        if(S.eq(buyerAddress.getIsdefault(),GlobalValues.FLAG_YES.value())){
            //当前用户为默认用户
            pBuyerAddressMapper.updateToNotDefault(me.getFuserid());
        }
        buyerAddress.setFuserid(me.getFuserid());
        buyerAddress.setCoid(me.getCoid());
        if(S.isNotBlank(buyerAddress.getGbid())){
            pBuyerAddressMapper.updateTemplateById(buyerAddress);
            return ResUtils.OK(buyerAddress.getGbid());
        }else {
            String gbid = S2.UUID();
            buyerAddress.setGbid(gbid);
            pBuyerAddressMapper.insert(buyerAddress);
            return ResUtils.OK(gbid);
        }
    }

    @Description("获取用户自己的默认收货地址")
    @GetAction("getdefault.json")
    @With(IHLogin.class)
    public String getDefault(
            @Description("选中的地址主键") String gbid
    ){
//        if(S.isNotBlank(gbid)){
//            return ResUtils.OK(pBuyerAddressMapper.single(gbid));
//        }
        PBuyerAddress buyerAddress = new PBuyerAddress();
        if(S.isNotBlank(gbid)){
            buyerAddress.setGbid(gbid);
        }else {
            buyerAddress.setFuserid(me.getFuserid());
        }
//        buyerAddress.setIsdefault(GlobalValues.FLAG_YES.value());
        buyerAddress.setCoid(me.getCoid());
        List<PBuyerAddress> addressList = pBuyerAddressMapper.template(buyerAddress);
        if(addressList!=null&&addressList.size()>0){
            return ResUtils.OK(addressList.get(0));
        }
        return ResUtils.OK(new PBuyerAddress());
    }

    @Description("获取收货地址")
    @GetAction("get.json")
    public String getAddress(
            @Description("地址主键")String gbid
    ){
        return ResUtils.OK(pBuyerAddressMapper.getAddressById(gbid));
    }

}
